﻿#include "protocolbuilder.h"

QByteArray Protocolbuilder::buildCmd(Commandcode cmd, const QByteArray& data) {
	QByteArray packet;
	QBuffer buffer(&packet);
	buffer.open(QIODevice::WriteOnly);
	QDataStream stream(&buffer);
	stream.setByteOrder(QDataStream::BigEndian);//数据流设置为大端模式

	stream.writeRawData(reinterpret_cast<const char*>(cmd_header), 2);

	stream << static_cast<quint8>(cmd);

	quint32 datalength = static_cast<quint32>(data.size());
	stream << datalength;
	if (!data.isEmpty()) {
		stream.writeRawData(data.constData(), data.size());
	}
	
	buffer.close();


	return packet;
}

QByteArray Protocolbuilder::buildPrintTextCmd(const QString& text,bool isImage) {
	QByteArray data;
	if (isImage) {
		data = QByteArray::fromBase64(text.toUtf8());
	}
	else {
		data = text.toLatin1();
	}
	return buildCmd(kCmdprint_text, data);
}
QByteArray Protocolbuilder::buildClearBufferCmd( int recordid)
{
	QByteArray data;
	QBuffer buffer(&data);
	QDataStream stream(&data, QIODevice::WriteOnly);
	stream.setByteOrder(QDataStream::BigEndian);
	stream << static_cast<quint32>(recordid);
	return buildCmd(kCmdclear_buffer, data);
}

QByteArray Protocolbuilder::buildReadUnPrintCmd() {
	QByteArray data = nullptr;
	return buildCmd(kCmdread_unprint,data);
}

QByteArray Protocolbuilder::buildQueryError() {
	QByteArray data = nullptr;
	return buildCmd(kCmdquery_error,data);
}

QByteArray Protocolbuilder::buildQueryStatus() {
	QByteArray data = nullptr;
	return buildCmd(kCmdquery_status,data);
}

QByteArray Protocolbuilder::buildStartPrint() {
	QByteArray data= nullptr;
	return buildCmd(kCmdstart_print,data);
}

QByteArray Protocolbuilder::buildStopPrint() {//停止喷印
	QByteArray data = nullptr;
	return buildCmd(kCmdstop_print, data);
}

QByteArray Protocolbuilder::buildClosePrint() {//停止打印删除缓存
	QByteArray data = nullptr;
	return buildCmd(kCmdpracache_stop, data);
}

QByteArray Protocolbuilder::buildStopApp() {//关闭软件
	QByteArray data = nullptr;
	return buildCmd(KCmdclose_app, data);
}

QByteArray Protocolbuilder::buildGetInkCommand() {//获取墨量
	QByteArray data = nullptr;
	return buildCmd(kCmdget_margin, data);
}

QByteArray Protocolbuilder::buildQueryOperate() {//查询当前操作状态
	QByteArray data = nullptr;
	return buildCmd(kCmdquery_operate, data);
}

QByteArray Protocolbuilder::buildQueryTemperature() {//查询墨盒温度
	QByteArray data = nullptr;
	return buildCmd(kCmdquery_temperature, data);
}

QByteArray Protocolbuilder::buildSetTemplate(const QString path) {//设置模板路径
	QByteArray data = path.toUtf8();
	return buildCmd(kCmdset_template, data);
}

QByteArray Protocolbuilder::buildCurrentTemplate() {//获取当前模板路径
	QByteArray data = nullptr;
	return buildCmd(kCmdcurrent_template, data);
}

QByteArray Protocolbuilder::buildReadSpeed() {//读传送带速度
	QByteArray data = nullptr;
	return buildCmd(kCmdread_speed, data);
}

QByteArray Protocolbuilder::buildPrintPage() {//读打印页数
	QByteArray data = nullptr;
	return buildCmd(kCmdprint_page, data);
}

QByteArray Protocolbuilder::buildCurrentSend() {//读当前发送的数据
	QByteArray data = nullptr;
	return buildCmd(kCmdcurrent_send, data);
}

QByteArray Protocolbuilder::buildPracacheStop() {//预缓存模式停止打印命
	QByteArray data = nullptr;
	return buildCmd(kCmdpracache_stop, data);
}

QByteArray Protocolbuilder::buildGetTemplateNum() {//读模板数
	QByteArray data = nullptr;
	return buildCmd(kCmdget_template_num, data);
}








